package com.comut.crud.bo;

import com.comut.crud.pojo.FindAndSet;
import com.comut.crud.dao.DriverDao;
import com.comut.crud.pojo.driver.Driver;
import com.comut.crud.ejb.interfaces.IDriverAction;
import com.comut.crud.pojo.mobile.DriverDetail;
import com.comut.crud.pojo.mobile.DriverInfo;
import com.comut.crud.util.ParseJson;
import com.comut.lib.connection.MongoDBConnectionManager;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.JSONMalFormedException;
import com.comut.lib.exception.ObjectFoundException;
import com.comut.lib.exception.ObjectNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class DriverHandler implements IDriverAction {

    private static Logger _logger = Logger.getLogger(DriverHandler.class.getName());
    private Connection _connection;
    private DriverDao driverDao;
    private final MongoDBConnectionManager _mongodb;

    public DriverHandler(Connection connection) {
        this._connection = connection;
        this._mongodb=null;
        driverDao = new DriverDao(_connection);        
    }

    /**
	 * @author Paulo Fournier
     * Issue: IFL-1569
     * Creates driver handler that uses an implementation of Connection and MongoDB
     * @param connection
     */
    public DriverHandler(Connection connection, MongoDBConnectionManager mongodb) {
        this._connection = connection;
        this._mongodb = mongodb;
        driverDao = new DriverDao(_connection,_mongodb);
    }
    
    
    public Driver insertDriver(Driver driver) throws ObjectFoundException {
        if (driver.isValid()) {
            if (driver.getDr_grp() != null && !driver.getDr_grp().isValid()) {
                throw new JSONMalFormedException("The mandatory attributes in subdocument are not in json.", ErrorCodes.JSON_MAL_FORMED);
            }
            int id = driverDao.createDriver(driver);
            Driver toRet = new Driver();
            toRet.setId(id);
            _logger.info(toRet.toString());
            return toRet;
        } else {
            throw new JSONMalFormedException("The mandatory attributes are not in json.", ErrorCodes.JSON_MAL_FORMED);
        }
    }

    public void deleteDriver(Driver driver) throws ObjectNotFoundException {
        _logger.info(driver.toString());
        driverDao.deleteDriver(driver.getId());
    }

    public void updateDriver(String driver) throws ObjectNotFoundException {
        _logger.info(driver.toString());
        //check if the BasicDBObject is well formed
        FindAndSet fas = ParseJson.getInstance().buildSQLQueryForDriver(driver, ParseJson.getInstance().getDriverFind(), ParseJson.getInstance().getDriverUpdate(),
                ParseJson.getInstance().getDriverFindStr(), ParseJson.getInstance().getDriverUpdateStr());
        //update the driver
        driverDao.updateDriver(fas);
    }

    public Driver getDriver(Driver driver) throws ObjectNotFoundException {

        ResultSet rs = driverDao.getDriver(driver.getId());
        Driver toRet = new Driver();
        try {
            if (rs.next()) {
                toRet.setId(driver.getId());
            }
        } catch (SQLException ex) {
            _logger.error(ex);
        }

        _logger.info(toRet.toString());
        return toRet;
    }

    /**
	 * @author Paulo Fournier
	 * Issue: IFL-1569
     * Gets list of drivers for user id.
	 * @param userID The user id
	 * @return DriverInfo[] The list of drivers and their content
     */
    public DriverInfo[] getListDrivers(int userId) throws ObjectNotFoundException {
        return driverDao.getDriversForUserId(userId);
    }

    /**
	 * @author Paulo Fournier
     * Issue: IFL-1570
	 * Get the detailed information of a driver
     * @param userId The user id
     * @param driverId The driver id
     * @return DriverDetail The detailed information
     * @throws ObjectNotFoundException 
     */
    public DriverDetail getDriverDetail(int userId, int driverId) throws ObjectNotFoundException {
        return driverDao.getDriverDetail( userId,  driverId);        
    }
}
